package com.ronx.scs.servlet;

import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FreeMarkerDispatcherServlet extends DispatcherServlet {
    private HttpServletRequest request;
    private HttpServletResponse response;
//    private final static int DEFAULT_BUFFER_SIZE = 1024 * 8;
    private boolean startFlag = true;

//    @Autowired
//    public Cache ehCacheFactoryBean;

    public void setStartFlag(final boolean startFlag) {
        this.startFlag = startFlag;
    }

    @Override
    public void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {
        this.request = request;
        this.response = response;
        if (startFlag) {
            aopCacheJoinPoint(request, response);
        } else {
            invokeSuperDoService(request, response);
        }
    }

    public void aopCacheJoinPoint(HttpServletRequest request, HttpServletResponse response) {
    }

    public void invokeSuperDoService(HttpServletRequest request, HttpServletResponse response) throws Exception {
        super.doService(request, response);
    }

//    public void aopCacheJoinPoint(HttpServletRequest request, HttpServletResponse response) throws IOException {
////        Object[] arguments = proceedingJoinPoint.getArgs();
//        setStartFlag(false);
//        String cacheKey = getCacheKey(proceedingJoinPoint);
//        Element element = ehCacheFactoryBean.get(cacheKey);
//        if (element == null) {
//            ServletRequest servletRequest = request;
//            HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
//            StringBuffer requestURL = httpRequest.getRequestURL();
//            URL url = new URL(requestURL.toString());
//            URLConnection urlConnection = url.openConnection();
////            urlConnection.setDoOutput(true);
//            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
//
////            String inputLine;
//            StringBuilder stringBuilder = new StringBuilder();
//
//            CharBuffer charBuffer = CharBuffer.allocate(DEFAULT_BUFFER_SIZE);
//
//            while (bufferedReader.read(charBuffer) >= 0) {
//                stringBuilder.append(charBuffer.flip());
//                charBuffer.clear();
//            }
//
//            byte[] bytes = stringBuilder.toString().getBytes();
//            bufferedReader.close();
//            element = new Element(cacheKey, bytes);
//            ehCacheFactoryBean.put(element);
////            System.out.println("PUT into cache " + new Date());
//        } else {
////            System.out.println("GET from cache " + new Date());
//        }
//        response.setContentType("text/html");
//        response.getOutputStream().write((byte[]) element.getObjectValue());
//        setStartFlag(true);
//    }
//
//    private String getCacheKey(ProceedingJoinPoint proceedingJoinPoint) {
//        Object[] arguments = proceedingJoinPoint.getArgs();
//        StringBuilder stringBuilder =
//            new StringBuilder(getClass().getSimpleName() + "." + proceedingJoinPoint.getSignature().getName());
//        if ((arguments != null) && (arguments.length != 0)) {
//            for (Object argument : arguments) {
//                stringBuilder.append(".").append(argument);
//            }
//        }
//        return stringBuilder.toString();
//    }
}
